package com.cicadalane.androlate;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


public class StringData {

	private boolean mHasIndex = false;
	private int mIndex = -1;
	private String mName;
	private String mValue;

	public StringData() {
		mName = "";
		mValue = "";
	}

	public StringData(String name, String value) {
		mName = name;
		mValue = value;
	}

	public int getIndex() throws Exception {
		if (!mHasIndex)
			throw new Exception("Index not set");
		return mIndex;
	}

	public String getName() {
		return mName;
	}

	/*
	 * compare, but ignore value
	 */
	public boolean isSimilar(Object obj) {
		if (!(obj instanceof StringData))
			return false;

		if (!(obj instanceof StringData))
			return false;

		StringData sd = (StringData) obj;
		if (hasIndex() || sd.hasIndex()) {
			if (hasIndex() && !sd.hasIndex())
				return false;
			if (!hasIndex() && sd.hasIndex())
				return false;

			try {
				return (getName().equals(sd.getName()) && getIndex() == sd
						.getIndex());
			} catch (Exception e) {
				return false;
			}
		} else {
			return (getName().equals(sd.getName()));
		}
	}

	public boolean nameEquals(Object obj) {
		if (!(obj instanceof StringData))
			return false;

		StringData sd = (StringData) obj;
		return (getName().equals(sd.getName()));
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof StringData))
			return false;

		StringData sd = (StringData) obj;
		if (hasIndex() || sd.hasIndex()) {
			if (hasIndex() && !sd.hasIndex())
				return false;
			if (!hasIndex() && sd.hasIndex())
				return false;

			try {
				return (getName().equals(sd.getName())
						&& getValue().equals(sd.getValue()) && getIndex() == sd
						.getIndex());
			} catch (Exception e) {
				return false;
			}
		} else {
			return (getName().equals(sd.getName()) && getValue().equals(
					sd.getValue()));
		}
	}

	public String getValue() {
		return mValue;
	}

	public boolean hasIndex() {
		return mHasIndex;
	}

	public void setIndex(int value) {
		mHasIndex = true;
		mIndex = value;
	}

	public void setName(String name) {
		mName = name;
	}

	public void setValue(String value) {
		mValue = value;
	}
}
